package com.wy2.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wy2.entity.Module;
import com.wy2.entity.RoleModule;
import com.wy2.mapper.ModuleMapper;
import com.wy2.mapper.RoleModuleMapper;
import com.wy2.service.ModuleService;
import com.wy2.util.FenyeUtil;

@RestController
@RequestMapping("/module")
@CrossOrigin
public class ModuleController {
	
	@Autowired
	private ModuleService ms;
	
	@Autowired
	private ModuleMapper mm;
	
	@Autowired
	private RoleModuleMapper rm;
	/**
	 * 树结构
	 * @param m
	 * @return
	 */
	@RequestMapping("/getTrees")
	public List<Module> getAll(Module m){
		//查询fid
		List<Module> pid = mm.findByParentId(0);
		//将module存入list集合中
		List<Module> menulist = new ArrayList<Module>();
		//new一个新的module对象
		Module md = new Module();
		Module child;
		//将module存入list集合中
		List<Module> children = new ArrayList<>();
		for(Module modules : pid) {
			md = new Module();
			Integer id = modules.getParentid();
			Integer mid = modules.getId();
			child = new Module();
			if(id == 0) {
				md = new Module(modules.getId(),modules.getTitle(),modules.getParentid(),modules.getPath(),
						modules.getWeight(),null);
				children = new ArrayList<>();
				
				List<Module> childList = mm.findByParentId(mid);
				for(Module modules2 : childList) {
					child = new Module(modules2.getId(),modules2.getTitle(),modules2.getParentid(),modules2.getPath(),
							modules2.getWeight(),null);
					children.add(child);
					md.setChildren(children);
				}
			}
			menulist.add(md);
		}
		return menulist;
	}
	/**
	 * 根据用户id查询所拥有的模块
	 * @param uid
	 * @return
	 */
	@RequestMapping("/getAll")
	public List<Module> getModule(String uid){
		System.out.println("----"+uid);
		return ms.getModuleByUser(uid);
	}
	
	//节点名称唯一
	@RequestMapping("/Txtname")
	public Integer getBytitle(String title) {
		List<Module> mlist = ms.findBytitle(title);
		if(mlist.size() > 0) {
			return 1;
		}else {
			return 0;
		}
	}
	/**
	 * 根据id获取全部
	 * @param id
	 * @return
	 */
	@RequestMapping("/getM")
	public Object getM(String id) {
		System.out.println("----"+id);
		Map<String, Object> map = new HashMap<String, Object>();
		Module m = ms.getM(id);
		if(m != null) {
			map.put("message", m);
			map.put("success", true);
		}
		return map;
	}
	/**
	 * 添加模块
	 * @param m
	 * @return
	 */
	@RequestMapping("/add")
	public Map<String, Object> addMod(Module m) {
		return ms.addMod(m);
	}
	/**
	 * 修改模块
	 * @param m
	 * @return
	 */
	@RequestMapping("/updMod")
	public Map<String, Object> updMod(Module m) {
		return ms.updMod(m);
	}
	/**
	 * 根据id删除
	 * @param id
	 * @return
	 */
	@RequestMapping("/del")
	public Object delMod(String id) {
		System.out.println("---id"+id);
		return ms.delMod(id);
	}
	/**
	 * 获取全部
	 * @param fenye
	 * @param module
	 * @return
	 */
	@RequestMapping("/all")
	public FenyeUtil<Module> getMod(FenyeUtil<Module> fenye,Module module){
		fenye.setT(module);
		fenye = ms.getMod(fenye);
		return fenye;
	}
	/**
	 * 查询模块角色中间表
	 * @param rid
	 * @return
	 */
	@RequestMapping("/getByMid")
	public List<Integer> getByMid(String rid){
		return rm.getByRid(rid);
	}
	
	
	@RequestMapping("/AddAndDel")
	public Map<String, Object> deleteRid(String rid,@RequestParam("id") List<Integer> id){
		System.out.println(rid);
		return ms.AddandDel(rid,id);
	}
}
